package com.king.juc.livelock;

import lombok.extern.slf4j.Slf4j;

/**
 * 活锁 演示案例
 *
 * @author king
 * @date 2020/11/20 16:12
 */
@Slf4j(topic = "c.LiveLockDemo")
public class LiveLockDemo {
    static volatile int count = 10;
    static final Object lock = new Object();

    public static void main(String[] args) {
        new Thread(() ->{
            // 期望减到0退出循环
            while(count > 0){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                --count;
                log.debug("count: {}", count);
            }
        }, "t1").start();
        new Thread(() ->{
            // 期望减到0退出循环
            while(count < 20){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ++count;
                log.debug("count: {}", count);
            }
        }, "t2").start();
    }
}
